﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BeachRunner.Tileengine
{
    /// <summary>
    /// Eine Klasse für ein animiertes und somit dynamisches Tileset.
    /// 
    /// Autor: OLD (Mattis Hänel)
    /// </summary>
    public class Tileset
    {
        /// <summary>
        /// Die zu verwendende Grafik.
        /// </summary>
        private Texture2D texture;
        /// <summary>
        /// Macht die zu verwendende Grafik zugänglich.
        /// </summary>
        public Texture2D Texture
        {
            get { return texture; }
        }

        /// <summary>
        /// Für jedes Frame des Tielsets ein Rect
        /// </summary>
        private Rectangle[] tileRectangles;

        /// <summary>
        /// Die Index der momentanen Frames aller Tiles
        /// </summary>
        private short[] currentTile;

        /// <summary>
        /// Gibt für jedes Frame an welches das nächste Frame sein wird.
        /// </summary>
        private short[] nextTile;

        /// <summary>
        /// Gibt für jedes Frame an wie lange es angezeigt wird.
        /// </summary>
        private short[] tileDuration;

        /// <summary>
        /// Gibt für jedes Frame an wie lange es noch angezeigt wird.
        /// </summary>
        private short[] durationLeft;

        /// <summary>
        /// Jedem Tile wird ein Rect für das Momentane Frame zugeordnet.
        /// </summary>
        private Rectangle[] sourceRectangles;
        public Rectangle[] SourceRectangles
        {
            get { return sourceRectangles; }
        }

        /// <summary>
        /// Erstellt ein neues Tileset
        /// </summary>
        /// <param name="texture">Die zu verwendende Textur</param>
        /// <param name="nextTile">Die AnimationsAbläufe</param>
        /// <param name="tileDuration">Die Animationszeiten</param>
        public Tileset(Texture2D texture, short[] nextTile, short[] tileDuration)
        {
            this.texture = texture;

            int tilesPerWidth = texture.Width / Game1.TILE_SIZE;
            int tilesPerHeight = texture.Height / Game1.TILE_SIZE;

            int length = tilesPerWidth * tilesPerHeight;
            tileRectangles = new Rectangle[length];
            sourceRectangles = new Rectangle[length];

            this.nextTile = nextTile;
            this.tileDuration = tileDuration;

            currentTile = new short[length];
            durationLeft = new short[length];

            int index = 0;
            for (int y = 0; y < tilesPerHeight; y++)
            {
                for (int x = 0; x < tilesPerWidth; x++)
                {
                    currentTile[index] = (short)index;
                    durationLeft[index] = 0;

                    tileRectangles[index] = new Rectangle(x * Game1.TILE_SIZE, y * Game1.TILE_SIZE, Game1.TILE_SIZE, Game1.TILE_SIZE);
                    sourceRectangles[index] = tileRectangles[index];
                    index++;
                }
            }
        }

        /// <summary>
        /// Updated das Tileset
        /// </summary>
        public void Update()
        {
            for (int i = 0; i < sourceRectangles.Length; i++)
            {
                if (nextTile[i] != -1)
                {
                    durationLeft[i] -= Game1.TIME_PER_TICK;
                    if (durationLeft[i] < 0)
                    {
                        currentTile[i] = nextTile[currentTile[i]];
                        sourceRectangles[i] = tileRectangles[currentTile[i]];

                        durationLeft[i] = tileDuration[currentTile[i]];
                    }
                }
            }
        }

    }
}
